package com.runshu.newsmartark.base;

import android.app.Application;

import com.arcsoft.face.FaceEngine;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.network.manager.NetManager;
import com.runshu.newsmartark.network.okhttpframework.data.OKHttpPullRequest;
import com.runshu.newsmartark.network.okhttpframework.frameworkimpl.OkHttpFramework;
import com.runshu.newsmartark.networkstate.NetworkBus;
import com.runshu.newsmartark.util.Beeper;
import com.runshu.newsmartark.util.CrashHandler;
import com.runshu.newsmartark.util.PreferenceUtil;
import com.runshu.newsmartark.util.UtilSharedPreference;
import com.runshu.newsmartark.util.common.Constants;
import com.runshu.newsmartark.util.common.ToastUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import cn.wch.ch34xuartdriver.CH34xUARTDriver;


public class MyApplication extends Application {
    public static MyApplication application;
    private UserInfoEntity userInfoEntity;
    public static CH34xUARTDriver driver;//JAR入口对象
    private FaceEngine ftEngine;
    public UserInfoEntity getUserInfoEntity() {
        userInfoEntity = UtilSharedPreference.getUserInfo(this);
        if (userInfoEntity == null){
            userInfoEntity = new UserInfoEntity();
        }
        return userInfoEntity;
    }

    public void setUserInfoEntity(UserInfoEntity userInfoEntity) {
        this.userInfoEntity = userInfoEntity;
        UtilSharedPreference.saveUserInfo(this, userInfoEntity);
    }

    @Override
    public void onCreate() {
        application = this;
        ToastUtils.init(this);
        PreferenceUtil.init(application);
        CrashHandler.getInstance().init();
        super.onCreate();
//        //语音合成
//        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5fcf204c");
        //初始化错误收集
        //初始化网络监听
        NetworkBus.getDefault().init(this);
        //初始化友盟
        initUM();
        //初始化网络加载框架
        NetManager.getInstance().initNetFramework(new OkHttpFramework(new OKHttpPullRequest()), this);
        try {
            Beeper.init(application);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        ftEngine = new FaceEngine();
        ftEngine.active(this, Constants.APP_ID, Constants.SDK_KEY);

    }

    private void initUM() {
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled( true );
        UMConfigure.init( this,  UMConfigure.DEVICE_TYPE_PHONE, null );
        MobclickAgent.setPageCollectionMode( MobclickAgent.PageMode.AUTO );
    }

    public static MyApplication getApplication() {
        return application;
    }

    /**
     * 清空登录信息
     */
    public void removeUserInfo() {
        this.userInfoEntity = null;
        UtilSharedPreference.clearUserInfo(this);
    }

    public static UserInfoEntity getUserInfoGlob() {
        return MyApplication.getApplication().getUserInfoEntity();
    }
}
